﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using DAL.EntityDataModel;
using BLL.Formateur;
using BLL.Formation;

namespace BLL.SessionFormation
{
    public class SessionFormationBLL
    {
        /*****************************
         * Public BLL Method
         *****************************/

        /// <summary>
        /// Return all formations
        /// </summary>
        /// <returns></returns>
        public static List<SessionFormationBO> GetSessionFormations()
        {
            List<SessionFormationBO> sessionFormationsBO = new List<SessionFormationBO>();
            List<DAL.EntityDataModel.SessionFormation> sessionFormations = new List<DAL.EntityDataModel.SessionFormation>();

            using (DBEntities db = new DBEntities())
            {

                try
                {
                    sessionFormations = db.SessionFormations.ToList();

                    sessionFormationsBO = SessionFormationBLL.CopyeEntitiesToBOlist(sessionFormations);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

            }

            return sessionFormationsBO;
        }

        public static void AddSessionFormations(SessionFormationBO bo)
        {
            DAL.EntityDataModel.SessionFormation entity = new DAL.EntityDataModel.SessionFormation();

            using(DBEntities db = new DBEntities())
            {
                try
                {
                    SessionFormationBLL.CopyBoToEntity(bo, entity);
                    db.SessionFormations.AddObject(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

        }

        
        public static void UpdateSessionFormations(SessionFormationBO bo)
        {
            using (DBEntities db = new DBEntities())
            {
                try
                {
                    DAL.EntityDataModel.SessionFormation entity = db.SessionFormations.FirstOrDefault(sf => sf.Identifiant == bo.Identifiant);
                    CopyBoToEntity(bo, entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }

        public static void DeleteSessionFormations(int id)
        {
            
            
            using(DBEntities db = new DBEntities())
            {
                try
                {
                    DAL.EntityDataModel.SessionFormation entity = db.SessionFormations.FirstOrDefault(f => f.Identifiant == id);        
                    db.SessionFormations.DeleteObject(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }

        /*****************************
         * Public traitement Method
         *****************************/

        /// <summary>
        /// Copy a entity to a business object
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static SessionFormationBO CopyEntityToBO(DAL.EntityDataModel.SessionFormation entity)
        {
            SessionFormationBO bo = new SessionFormationBO();

            try
            {
                bo.DateDebut = entity.DateDebut;
                bo.DateFin = entity.DateFin;
                bo.Formateur = FormateurBLL.CopyEntityToBO(entity.Formateur);
                bo.IdCommercial = entity.IdCommercial;
                bo.Identifiant = entity.Identifiant;
                bo.Inter = entity.Inter;
                bo.Formation = FormationBLL.CopyEntityToBO(entity.Formation);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        /// <summary>
        /// Copy Business to entity
        /// </summary>
        /// <param name="bo"></param>
        /// <returns></returns>
        public static void CopyBoToEntity(SessionFormationBO bo, DAL.EntityDataModel.SessionFormation entity)
        {
            try
            {
                if (bo.Identifiant != 0)
                    entity.Identifiant = bo.Identifiant;

                entity.DateDebut = bo.DateDebut;
                entity.DateFin = bo.DateFin;

                if (bo.Formateur != null)
                    entity.IdFormateur = bo.Formateur.Identifiant;

                if (bo.Formation != null)
                    entity.IdFormation = bo.Formation.Identifiant;    
                
                entity.IdCommercial = bo.IdCommercial;
                entity.Inter = bo.Inter;

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// Copy entities to a list of business object
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static List<SessionFormationBO> CopyeEntitiesToBOlist(List<DAL.EntityDataModel.SessionFormation> entities)
        {
            List<SessionFormationBO> boList = new List<SessionFormationBO>();

            try
            {
                foreach (DAL.EntityDataModel.SessionFormation entity in entities)
                {
                    SessionFormationBO bo = SessionFormationBLL.CopyEntityToBO(entity);
                    boList.Add(bo);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return boList;
        }
    }
}
